home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / TEXTEDIT.SWG / 0006_WORDWRP2.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  2KB  |  89 lines

  1. {
  2. >    P.S.  A pre-made Unit to do a Word-wrap Function might also be nice.
  3. }
  4.  
  5. Unit WordWrap;
  6.  
  7. Interface
  8.  
  9. Uses
  10.   Crt;
  11.  
  12. Type
  13.   Strn80 = String[80];
  14.  
  15. Const
  16.   MaxWordLineLength : Byte = 80;
  17.  
  18. Var
  19.   WordLine  : Strn80;
  20.   Index1    : Byte;
  21.   Index2    : Byte;
  22.  
  23. Procedure ResetWrapStrn;
  24. Procedure WrapStrn (InputStrn: Strn80);
  25.  
  26. Implementation
  27.  
  28. Procedure ResetWrapStrn;
  29. begin
  30.   Index1 := 0;
  31.   Index2 := 0;
  32.   Wordline := '';
  33. end;
  34.  
  35. Procedure WrapStrn (InputStrn: Strn80);
  36. Var
  37.   Count : Byte;
  38.   InputChar : Char;
  39. begin
  40.   For Count := 1 to Length (InputStrn) do
  41.   begin
  42.     InputChar := InputStrn[Count];
  43.     Case InputChar OF
  44.       ^H: {Write destructive backspace & remove Char from WordLine}
  45.           begin
  46.             Write(^H,' ',^H);
  47.             DELETE(WordLine,(LENGTH(WordLine) - 1),1)
  48.           end;
  49.       #0: {user pressed a Function key, so dismiss it}
  50.           begin
  51.             InputChar := ReadKey; {Function keys send two-Char scan code!}
  52.             InputChar := ' '
  53.           end;
  54.       #13: { it is an enter key.. reset everything and start on a new line}
  55.           begin
  56.             Writeln;
  57.             Index1 := 0; Index2 := 0; Wordline := '';
  58.           end;
  59.       else {InputChar contains a valid Char, so deal With it}
  60.       begin
  61.         Write(InputChar);
  62.         WordLine := (WordLine + InputChar);
  63.         if (LENGTH(WordLine) >= (MaxWordLineLength - 1)) then
  64.         {we have to do a Word-wrap}
  65.         begin
  66.           Index1 := (MaxWordLineLength - 1);
  67.           While ((WordLine[Index1] <> ' ') and (WordLine[Index1] <> '-')
  68.                   and (Index1 <> 0)) DO
  69.             Index1 := (Index1 - 1);
  70.           if (Index1 = 0) then {whoah, no space was found to split line!}
  71.             Index1 := (MaxWordLineLength - 1); {forces split}
  72.           DELETE(WordLine,1,Index1);
  73.           For Index2 := 1 to LENGTH(WordLine) DO
  74.             Write(^H,' ',^H);
  75.           Writeln;
  76.           Write(WordLine)
  77.         end
  78.       end
  79.     end; {CASE InputChar}
  80.   end;
  81. end;
  82.  
  83. begin {WordWrap}
  84. {Initialize the Program.}
  85. WordLine  := '';
  86. Index1    := 0;
  87. Index2    := 0;
  88. end.
  89.